package org.cttic.visualization.visualization.z_service.impl;

import cn.hutool.json.JSONObject;
import org.cttic.visualization.visualization.common.AssistChargingRequest;
import org.cttic.visualization.visualization.common.AssistChargingResponse;
import org.cttic.visualization.visualization.util.ResponseObject;
import org.cttic.visualization.visualization.z_service.AssistChargingTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class AssistChargingServiceImpl implements AssistChargingTimeService {

    private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:00:00");

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public ResponseObject assistCharging(AssistChargingRequest request) {

        String queryType = request.getQueryType();

        String provinceId = request.getProvinceId();

        String pointType = request.getPointType();

        List<AssistChargingResponse> list = new ArrayList<>();

        boolean etc = false;

        if("0".equals(pointType)){
            etc = true;
        }


        if("0".equals(queryType)){


        }else if("1".equals(queryType)){    //当前第一阶段省级大屏，会按时间段查询

            LocalDateTime now = LocalDateTime.now();
            String format = formatter.format(now);

            String format1 = formatter.format(now.minusHours(8));




            if(etc){

                String s = this.buildSql(format1, format, "1,2");
                AssistChargingResponse assistEtc = this.findData(s,"4");
                list.add(assistEtc);
                String s3 = this.buildSql(format1, format, "3");
                AssistChargingResponse assistStation = this.findData(s3,"3");
                list.add(assistStation);

            }else {
                //普通门架
                String s = this.buildSql(format1, format, "1");
                AssistChargingResponse assistEtc = this.findData(s,"1");
                list.add(assistEtc);
                //省界门架
                String s2 = this.buildSql(format1, format, "2");
                AssistChargingResponse assistEtcP = this.findData(s,"2");
                list.add(assistEtcP);
                //收费站
                String s3 = this.buildSql(format1, format, "3");
                AssistChargingResponse assistStation = this.findData(s3,"3");
                list.add(assistStation);
            }


        }


        JSONObject res = new JSONObject().put("pointTypeArray",list);

        return ResponseObject.success(res);
    }

    private String buildSql(String startTime,String endTime,String type){
        String sql = "SELECT sum(total) c1,`hour` c2  FROM `bd_up_req_message` " +
            " where `hour`>= '"+startTime+"' and `hour`<= '"+endTime+"' and pointType in ("+type+") " +
            " GROUP BY `hour`  ORDER BY `hour`";
        return sql;
    }

    private AssistChargingResponse findData(String sql,String type){
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);

        List<AssistChargingResponse.HourRatio> ratioList = new ArrayList<>();

        for (Map<String, Object> map : maps) {
            AssistChargingResponse.HourRatio ratio = new AssistChargingResponse.HourRatio();

            Object c2 = map.get("c2");
            LocalDateTime n = LocalDateTime.parse(c2.toString(),formatter);
            int hour = n.getHour();
            hour++;

            ratio.setServiceNums(map.get("c1").toString());
            ratio.setHourPeriod(hour+"");

            ratioList.add(ratio);
        }
        AssistChargingResponse ass = new AssistChargingResponse();
        ass.setPointType(type);
        ass.setHourRatio(ratioList);

        return ass;
    }

}
